package com.timemanager.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.timemanager.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 日期选择器
 */
public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView titleText;
    private Button btnToHis;
    private StringBuilder fromDate = new StringBuilder();
    private StringBuilder toDate = new StringBuilder();
    private Calendar calendar=Calendar.getInstance();
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private int year, monthOfYear, dayOfMonth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker);
        titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText("选择查询时间范围");
        DatePicker datePicker=(DatePicker)findViewById(R.id.from_date);
        year=calendar.get(Calendar.YEAR);
        monthOfYear=calendar.get(Calendar.MONTH);
        dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        fromDate.append(format.format(calendar.getTime()));
        datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
            public void onDateChanged(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                calendar.set(year, monthOfYear, dayOfMonth);
                fromDate.delete(0, toDate.length());
                fromDate.append(format.format(calendar.getTime()));
            }
        });

        DatePicker datePicker2=(DatePicker)findViewById(R.id.to_date);
        final Calendar calendar2=Calendar.getInstance();
        int year2=calendar2.get(Calendar.YEAR);
        int monthOfYear2=calendar2.get(Calendar.MONTH);
        int dayOfMonth2=calendar2.get(Calendar.DAY_OF_MONTH);
        toDate.append(format.format(calendar2.getTime()));
        datePicker2.init(year2, monthOfYear2, dayOfMonth2, new DatePicker.OnDateChangedListener(){
            public void onDateChanged(DatePicker view, int year2,
                                      int monthOfYear2, int dayOfMonth2) {
                calendar2.set(year2, monthOfYear2, dayOfMonth2);
                toDate.delete(0, toDate.length());
                toDate.append(format.format(calendar2.getTime()));
            }
        });
        btnToHis = (Button) findViewById(R.id.btn_to_his);
        btnToHis.setOnClickListener(this);
    }

    public void onClick(View v) {
        if (v.getId() == R.id.btn_to_his) {
            if (fromDate.length() > 0 && toDate.length() > 0) {
                if (fromDate.toString().compareTo(toDate.toString()) > 0) {
                    showDateErrorDialog();
                } else {
                    Intent intent = new Intent(DatePickerActivity.this, HistoryEventTimeActivity.class);
                    intent.putExtra("from_date", fromDate.toString());
                    intent.putExtra("to_date", toDate.toString());
                    startActivity(intent);
                }
            } else {
                Toast.makeText(DatePickerActivity.this, "请选择日期。", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void showDateErrorDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(DatePickerActivity.this);
        builder.setTitle("日期错误");
        builder.setMessage("查询结束日期必须在开始日期之后");
        builder.setPositiveButton("重新选择日期", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }
}
